cmake_minimum_required(VERSION 3.0.0)
project(radar VERSION 0.2)

include(CTest)
enable_testing()

message(STATUS "${PROJECT_NAME} started building on ${CMAKE_SYSTEM_NAME}!")

# 模式选择，是否选择正常运行模式
if (WIN32)
    set(use_normal_mode false)
else (WIN32)
    set(use_normal_mode true)
endif (WIN32)

if (use_normal_mode)
    # Options normal
    set(use_camera_usb2 false)     # 是否使用电脑自带摄像头做调试
    set(camera_id 0)
    # 是否使用虚拟Serial
    set(use_fake_ttl false)
    #set(use_fake_ttl true)
    # 是否编译装甲板
    #set(compile_armor_detect false)
    set(compile_armor_detect true)
    # 是否包含CUDA
    #set(compile_cuda false)
    set(compile_cuda true)
    # 是否PNP
    set(compile_pnp true)
    #set(compile_pnp false)
else (use_normal_mode)
    # Options simple
    set(use_camera_usb2 false)     # 是否使用电脑自带摄像头做调试
    set(camera_id 0)
    # 是否使用虚拟Serial
    #set(use_fake_ttl false)
    set(use_fake_ttl true)
    # 是否编译装甲板
    set(compile_armor_detect false)
    #set(compile_armor_detect true)
    # 是否包含CUDA
    set(compile_cuda true)
    #set(compile_cuda true)
    # 是否PNP
    #set(compile_pnp true)
    set(compile_pnp false)
    set(compile_pnp false)
    set(compile_pnp false)
endif (use_normal_mode)

configure_file(
        "${PROJECT_SOURCE_DIR}/include/compile_config.h.in"
        "${PROJECT_SOURCE_DIR}/include/compile_config.h"
)

# Packages
# cuda
if (compile_cuda)
    find_package(CUDA REQUIRED)
    include_directories(/usr/local/cuda/include)
    link_directories(/usr/local/cuda/lib64)
    include_directories(~/CLionProjects/tiny-tensorrt)
    link_directories(~/CLionProjects/tiny-tensorrt/build)

endif (compile_cuda)

# OpenCV
if (WIN32)
    message("Building on windows!")
    set(OpenCV_DIR D:\\Tools\\mingw64\\opt\\x64\\mingw\\lib)
endif ()
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV include dirs: ${OpenCV_INCLUDE_DIRS}, lib: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})
# glog
find_package(glog REQUIRED)

# Include
if (compile_armor_detect)
    set(include_dirs
            "${PROJECT_SOURCE_DIR}/include"
            "${PROJECT_SOURCE_DIR}/src/armor/include" "${PROJECT_SOURCE_DIR}/src/armor/src"
            "${PROJECT_SOURCE_DIR}/src/mapping/include" "${PROJECT_SOURCE_DIR}/src/mapping"
            "${PROJECT_SOURCE_DIR}/src/rmcamera/include" "${PROJECT_SOURCE_DIR}/src/rmcamera"
            "${PROJECT_SOURCE_DIR}/src/rmconfig/include" "${PROJECT_SOURCE_DIR}/src/rmconfig"
            "${PROJECT_SOURCE_DIR}/src/minimap/include" "${PROJECT_SOURCE_DIR}/src/minimap"
            "${PROJECT_SOURCE_DIR}/src/com/include" "${PROJECT_SOURCE_DIR}/src/com"
            "${PROJECT_SOURCE_DIR}/src/net/include"
            "${PROJECT_SOURCE_DIR}/src/others/include")
else (compile_armor_detect)
    set(include_dirs
            "${PROJECT_SOURCE_DIR}/include"
            "${PROJECT_SOURCE_DIR}/src/armor/include" "${PROJECT_SOURCE_DIR}/src/armor/src"
            "${PROJECT_SOURCE_DIR}/src/mapping/include" "${PROJECT_SOURCE_DIR}/src/mapping"
            "${PROJECT_SOURCE_DIR}/src/rmcamera/include" "${PROJECT_SOURCE_DIR}/src/rmcamera"
            "${PROJECT_SOURCE_DIR}/src/rmconfig/include" "${PROJECT_SOURCE_DIR}/src/rmconfig"
            "${PROJECT_SOURCE_DIR}/src/minimap/include" "${PROJECT_SOURCE_DIR}/src/minimap"
            "${PROJECT_SOURCE_DIR}/src/com/include" "${PROJECT_SOURCE_DIR}/src/com"
            "${PROJECT_SOURCE_DIR}/src/others/include")
endif (compile_armor_detect)
message(STATUS "Include dirs: ${include_dirs}")
include_directories(${include_dirs})

# Src

file(GLOB_RECURSE source_files
        #        "${PROJECT_SOURCE_DIR}/include/*.h" "${PROJECT_SOURCE_DIR}/include/*.hpp" # "${PROJECT_SOURCE_DIR}/include/instances.cpp"
        "${PROJECT_SOURCE_DIR}/src/**/include/*.h" "${PROJECT_SOURCE_DIR}/src/**/src/*.?pp"

        "${PROJECT_SOURCE_DIR}/src/com/include/*.h" "${PROJECT_SOURCE_DIR}/src/com/include/serial/*.h"
        "${PROJECT_SOURCE_DIR}/src/com/include/serial/impl/*.h"
        "${PROJECT_SOURCE_DIR}/src/com/src/*.cc" "${PROJECT_SOURCE_DIR}/src/com/src/*.cpp"
        "${PROJECT_SOURCE_DIR}/src/com/src/impl/*.cc" "${PROJECT_SOURCE_DIR}/src/com/src/impl/list_ports/*.cc"

        "${PROJECT_SOURCE_DIR}/src/rmcamera/include/*" "${PROJECT_SOURCE_DIR}/src/rmcamera/src/*"
        "${PROJECT_SOURCE_DIR}/src/minimap/include/*" "${PROJECT_SOURCE_DIR}/src/minimap/src/*"

        #        "${PROJECT_SOURCE_DIR}/src/**/*.?pp"
        "${PROJECT_SOURCE_DIR}/src/others/include/**/*.h" "${PROJECT_SOURCE_DIR}/src/others/src/*.?pp"
        "${PROJECT_SOURCE_DIR}/src/others/src/camera/*.?pp"
        "${PROJECT_SOURCE_DIR}/src/others/src/lib_json/*.?pp"
        "${PROJECT_SOURCE_DIR}/src/armor/src/armor_finder/armor_finder.cpp"
        "${PROJECT_SOURCE_DIR}/src/mapping/src/3Dpnp.cpp" "${PROJECT_SOURCE_DIR}/src/mapping/include/3Dpnp.h"
        "${PROJECT_SOURCE_DIR}/src/net/include/*.h" "${PROJECT_SOURCE_DIR}/src/net/include/serial/*.h")
message(STATUS "Source files: ${source_files}")

if (${use_camera_usb2})
    list(REMOVE_ITEM source_files "${PROJECT_SOURCE_DIR}/src/others/include/camera/cam_wrapper.h")
    list(REMOVE_ITEM source_files "${PROJECT_SOURCE_DIR}/src/others/src/camera/cam_wrapper.cpp")
endif ()

# Resources

file(GLOB_RECURSE resources "${PROJECT_SOURCE_DIR}/resource/*")
message(STATUS "Copying resources file(s): ${resources}")
file(COPY ${resources} DESTINATION ${CMAKE_BINARY_DIR}/)

# Linking

link_libraries(-lpthread glog::glog ${OpenCV_LIBS})
if (${use_camera_usb2})
    if (WIN32)
        message(STATUS "Linking lib on windows...")
        link_libraries("GxIAPICPPEx.lib")
        link_libraries("GxIAPI.lib")
        link_libraries("DxImageProc.lib")
        link_libraries(-lsetupapi)
    elseif (UNIX)
        message(STATUS "Linking lib on linux...")
        link_libraries(-lgxiapi)
    endif ()
endif ()

add_executable(radar ${PROJECT_SOURCE_DIR}/programs/main.cpp ${source_files})
if (compile_pnp)
    add_executable(radar_pnp ${PROJECT_SOURCE_DIR}/programs/pnp_test.cpp ${source_files})
endif (compile_pnp)
add_executable(new-serial-test ${PROJECT_SOURCE_DIR}/programs/new-serial-test.cpp ${source_files})
add_executable(rm-reference-test ${PROJECT_SOURCE_DIR}/programs/rm-reference-test.cpp ${source_files})
add_executable(ui-test ${PROJECT_SOURCE_DIR}/programs/ui-test.cpp ${source_files})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
